﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCY.CTP.DBShangZheng;
using System.Runtime.Serialization;
using IFServerDLL.DB;
using IFClientDLL.MyCalc;
using IFServerDLL.Model;

namespace HCY.CTP.DBShangZheng
{
    public class MyIF
    {

        public MyIF(MyDBData data, MyIF prevData)
        {
            _PrevData = prevData;
            _OpenPrice = data.OpenPrice;
            _YesterdayClosePrice = data.YesterdayClosePrice;
            _YesterdaySettlementPrice = data.YesterdaySettlementPrice;
            _RiseLimitPrice = data.RiseLimitPrice;
            _FallLimitPrice = data.FallLimitPrice;
            _YesterdayVolume = data.YesterdayVolume;
            _HeYue = data.Instrument;

            _MyServerTime = data.MyServerTime;
            _MarketTime = data.MarketTime;
            _HighPrice = data.HighPrice;
            _LowPrice = data.LowPrice;
            _SellPrice = data.SellPrice;
            _BuyPrice = data.BuyPrice;
            _BuyVolume = data.BuyVolume;
            _SellVolume = data.SellVolume;
            _NewPrice = data.NewPrice;
            _AvgPrice = Math.Round(data.AvgPrice / 300, 1);
            _SettlementVolume = data.SettlementVolume;
            _TradeVolume = data.TradeVolume;

            if (prevData == null)
            {
                _DifAvgPrice = 0;
                _DifBuyPrice = 0;
                _DifBuyVolume = 0;
                _DifHighPrice = 0;
                _DifLowPrice = 0;
                _DifMyServerTime = 0;
                _DifNewPrice = 0;
                _DifSellPrice = 0;
                _DifSellVolume = 0;
                _DifSettlementVolume = 0;
                _DifTradeVolume = 0;
            }
            else
            {
                _DifAvgPrice = _AvgPrice - prevData.AvgPrice;
                _DifBuyPrice = BuyPrice - prevData.BuyPrice;
                _DifBuyVolume = BuyVolume - prevData.BuyVolume;
                _DifHighPrice = HighPrice - prevData.HighPrice;
                _DifLowPrice = LowPrice - prevData.LowPrice;
                _DifMyServerTime = _MyServerTime - prevData.MyServerTime;
                _DifNewPrice = NewPrice - prevData.NewPrice;
                _DifSellPrice = SellPrice - prevData.SellPrice;
                _DifSellVolume = SellVolume - prevData.SellVolume;
                _DifSettlementVolume = SettlementVolume - prevData.SettlementVolume;
                _DifTradeVolume = TradeVolume - prevData.TradeVolume;
            }
        }
        public decimal GetPrice(MyEnumDirection direction, int chaojia)
        {
            if (direction == MyEnumDirection.Buy)
            {
                return _BuyPrice + MyVariety.GetVarietyByInstrument(_HeYue).OneChangedPrice * chaojia;
            }
            else
            {
                return _SellPrice - MyVariety.GetVarietyByInstrument(_HeYue).OneChangedPrice * chaojia;
            }
        }
        public int GetTimePct()
        {
            return Convert.ToInt32(_MyServerTime / 658);
        }
        #region 基本字段
        private MyIF _PrevData;
        public MyIF PrevData { get { return _PrevData; } }
        private string _HeYue;

        private decimal _OpenPrice;
        private decimal _YesterdayClosePrice;
        private decimal _YesterdaySettlementPrice;
        private decimal _RiseLimitPrice;
        private decimal _FallLimitPrice;
        private int _YesterdayVolume;
        public decimal OpenPrice { get { return _OpenPrice; } }
        public decimal YesterdayClosePrice { get { return _YesterdayClosePrice; } }
        public decimal YesterdaySettlementPrice { get { return _YesterdaySettlementPrice; } }
        public decimal RiseLimitPrice { get { return _RiseLimitPrice; } }
        public decimal FallLimitPrice { get { return _FallLimitPrice; } }
        public int YesterdayVolume { get { return _YesterdayVolume; } }

        public string Instrument { get { return _HeYue; } }



        private int _MyServerTime;
        public int MyServerTime { get { return _MyServerTime; } }
        private decimal _HighPrice;
        public decimal HighPrice { get { return _HighPrice; } }
        private decimal _LowPrice;
        public decimal LowPrice { get { return _LowPrice; } }
        private decimal _SellPrice;
        public decimal SellPrice { get { return _SellPrice; } }
        private decimal _BuyPrice;
        public decimal BuyPrice { get { return _BuyPrice; } }
        private decimal _NewPrice;
        public decimal NewPrice { get { return _NewPrice; } }
        private decimal _AvgPrice;
        public decimal AvgPrice { get { return _AvgPrice; } }
        private int _SellVolume;
        public int SellVolume { get { return _SellVolume; } }
        private int _BuyVolume;
        public int BuyVolume { get { return _BuyVolume; } }
        private int _SettlementVolume;
        public int SettlementVolume { get { return _SettlementVolume; } }
        private int _TradeVolume;
        public int TradeVolume { get { return _TradeVolume; } }
        private DateTime _MarketTime;
        public DateTime MarketTime { get { return _MarketTime; } }
        #endregion

        #region 变更字段
        private decimal _DifNewPrice;
        private int _DifMyServerTime;
        private decimal _DifHighPrice;
        private decimal _DifLowPrice;
        private decimal _DifSellPrice;
        private decimal _DifBuyPrice;
        private int _DifBuyVolume;
        private int _DifSellVolume;
        private decimal _DifAvgPrice;
        private int _DifSettlementVolume;
        private int _DifTradeVolume;

        public decimal DifNewPrice { get { return _DifNewPrice; } }
        public int DifMyServerTime { get { return _DifMyServerTime; } }
        public decimal DifHighPrice { get { return _DifHighPrice; } }
        public decimal DifLowPrice { get { return _DifLowPrice; } }
        public decimal DifSellPrice { get { return _DifSellPrice; } }
        public decimal DifBuyPrice { get { return _DifBuyPrice; } }
        public int DifBuyVolume { get { return _DifBuyVolume; } }
        public int DifSellVolume { get { return _DifSellVolume; } }
        public decimal DifAvgPrice { get { return _DifAvgPrice; } }
        public int DifSettlementVolume { get { return _DifSettlementVolume; } }
        public int DifTradeVolume { get { return _DifTradeVolume; } }
        #endregion

        #region 基本方法

        #endregion
        public override string ToString()
        {
            //string value = "→";
            //if (_DifNewPrice > 0)
            //{
            //    value = "↑";
            //}
            //else if (_DifNewPrice < 0)
            //{
            //    value = "↓";
            //}
            return _MarketTime.ToLongTimeString();// +" NewP:" + _NewPrice.ToString("0.0") + value + " Volume:" + _DifTradeVolume;
        }
        private List<CalcMsg> _listMsg = new List<CalcMsg>();
        public List<CalcMsg> ListCalcMsg { get { return _listMsg; } }
        public void AddCalcMsg(CalcMsg result)
        {
            _listMsg.Add(result);
        }
    }
    public class CalcMsg : ICalcMsg
    {
        private ICalcBase _calc;
        public CalcMsg(ICalcBase calc, string msg)
        {
            _calc = calc;

            _msg = msg;


        }
        private string _msg;
        public string Msg
        {
            get { return _msg; }
        }
        public Guid CalcID
        {
            get { return _calc.CalcID; }
        }

        public MyCalcType CalcType
        {
            get { return _calc.CalcType; }
        }

        public string CalcName
        {
            get { return _calc.CalcName; }
        }


        public string CalcDescripte
        {
            get { return _calc.CalcDescripte; }
        }
    }
    public interface ICalcMsg: ICalcBase
    {
        string Msg { get; }
    }
}
